package com.google.code.bankagregator.adapter.bank.forum.parser;

import com.google.code.bankagregator.adapter.exception.AdapterConnectException;
import com.google.code.bankagregator.entities.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: kirill
 * Date: 15.06.13
 * Time: 20:08
 * To change this template use File | Settings | File Templates.
 */
public class FuncForDepParser {
    private Deposit deposit;
    private Document con = null;
    private Bank bank = null;
    private static final String SELECT_PARAM = "div[class=section clearfix] ul li";
    private static final int INDEX_TD_WITH_RATE = 1;
    private static final int INDEX_LI_WITH_MIN_AMOUNT = 1;

    public FuncForDepParser(String url) throws AdapterConnectException {
        bank = new Bank();
        bank.setName("ПАТ \"БАНК ФОРУМ\"");
        bank.setUrl("http://www.forum.ua/forumua/ua/mainnavigation/home/home.html");
        try {
            con = Jsoup.connect(url).timeout(5000).get();
        } catch (IOException e) {
            throw new AdapterConnectException(e.getMessage());
        }
        deposit = new Deposit();
    }

    /**
     * parse params
     *
     * @param indexRateTR     - index TR with rate
     * @param indexMinAmount  - index current currency
     * @param currency        - current currency
     * @param state           - deposit state
     * @param depUrl          - deposit URL
     * @param indexLiWithAm   - index LI with amount
     * @param indexWithRefill - index current refill
     * @return deposit params
     * @throws AdapterConnectException
     */
    public List<Deposit> parser(int indexRateTR, int indexMinAmount, Currency currency, State state, String depUrl,
                                int indexLiWithAm, int indexWithRefill) throws AdapterConnectException {
        List<Deposit> depositParam = new ArrayList<>();
        Document con = null;
        try {
            con = new FuncForCredParser(depUrl).con;
        } catch (AdapterConnectException e) {
            throw new AdapterConnectException(e.getMessage());
        }
        Element rateEl = con.select("table.grid tbody tr").get(indexRateTR).
                select("td").get(INDEX_TD_WITH_RATE);
        String rateStr = rateEl.text().replace(",", ".").replace("%", "");

        Element minAmountEl = con.select(SELECT_PARAM).get(INDEX_LI_WITH_MIN_AMOUNT);
        String minAmountAr[] = minAmountEl.text().split(" ");

        Element refillEl = con.select(SELECT_PARAM).get(indexLiWithAm);
        String refillAr[] = refillEl.text().split(" ");

        Element name = con.select("h2").first();

        if ((refillAr[indexWithRefill].equals("можливо;")) || (refillAr[indexWithRefill].equals("можливість"))) {
            deposit.setRefill(true);
        } else deposit.setRefill(false);

        int minAmount = Integer.parseInt(minAmountAr[indexMinAmount]);
        double rate = Double.parseDouble(rateStr);

        deposit.setName(name.text());
        deposit.setUrl(depUrl);
        deposit.setMinAmount(minAmount);
        deposit.setDepositRate(rate);
        deposit.setCurrency(currency);
        deposit.setState(state);
        deposit.setBank(bank);
        deposit.setType(DepositType.Deposit);
        deposit.setTerm(372);
        depositParam.add(deposit);
        return depositParam;
    }
}
